package rowan.application.quickaccess.rss.parser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import rowan.application.quickaccess.rss.ImageDownloader;


/**
 * This class is the handler for the XML Reader used in Xmlparser
 * 
 * 
 * @author Tom
 *
 */
public class RowanRSSHandler extends DefaultHandler {
	
	// fields
	private boolean titleTag = false;
	private boolean linkTag = false;
	private boolean descTag = false;
	private String mTitle="", mLink="", mDesc ="";
	
	private ParsedDataSet myParsedDataSet = new ParsedDataSet();
	
	// Getter
	public ParsedDataSet getParsedData() {
		return myParsedDataSet;
	}
	
	// methods 
	@Override
	public void startDocument() throws SAXException {
		myParsedDataSet = new ParsedDataSet();
	}
	
	@Override
	public void endDocument() throws SAXException {
		//does nothing?
	}
	
	/**
	 * Gets be called on opening tags like:
	 * <tag>
	 * Can provide attributes(s), when xml is like:
	 * <tag attribute="attributeValue">
	 * 
	 * Turns field true and then calls the method characters to get what is inside the element
	 * This checks to see what the element IS
	 *  i.e. <p> or <div id="lololol"> 
	 */
	 @Override 
     public void startElement(String namespaceURI, String localName, 
               String qName, Attributes atts) throws SAXException { 
          if (localName.equals("title")) { 
               this.titleTag = true; 
          }else if (localName.equals("link")&& namespaceURI.length()==0) { //namespace URI is for the sports feed, or else we would have some stupid extra link
               this.linkTag = true; 
          }else if (localName.equals("description")) { 
               this.descTag = true;  	
          }else if (localName.equals("enclosure")) {
        	  	String imgUrl = atts.getValue("url");
        	  	byte[] image = new ImageDownloader().getBytesFromUrl(imgUrl);
        	  	myParsedDataSet.addImg(image);
//        	  	myParsedDataSet.addImgUrl(imgUrl);
        	  	
        	  	
        	  	
        	  	
										//          }else if (localName.equals("tagwithnumber")) { 
										//               // Extract an Attribute 
										//               String attrValue = atts.getValue("thenumber"); 
										//               int i = Integer.parseInt(attrValue); 
										//               myParsedExampleDataSet.setExtractedInt(i); 
               
          } 
     } 
	
	/** Gets be called on closing tags like:
	 * </tag>
	 */
	@Override 
    public void endElement(String namespaceURI, String localName, String qName) 
              throws SAXException { 
         if (localName.equals("title")) { 
        	  myParsedDataSet.addTitle(mTitle);
        	  mTitle = "";
              this.titleTag = false; 
         }else if (localName.equals("link") && this.linkTag) { 
        	 myParsedDataSet.addLink(mLink);
        	 mLink = "";
        	 this.linkTag = false;
         }else if (localName.equals("description")) { 
        	  myParsedDataSet.addDesc(mDesc);
        	  mDesc="";
              this.descTag = false; 
//         }else if (localName.equals("tagwithnumber")) { 
              // Nothing to do here 
         } 
    } 
	
	/** Gets be called on the following structure: 
     * <tag>characters</tag> */ 
    @Override 
   public void characters(char ch[], int start, int length){ 
    	if (titleTag ||  linkTag || descTag){
    	
        if(this.titleTag){
        	mTitle = mTitle + new String(ch, start, length);
//        	ab = ab.trim();
//        	myParsedDataSet.addTitle(ab);
        }
        else if(this.linkTag){
    		mLink = mLink + new String(ch, start, length);
    	}
        else if(this.descTag){ 
        	 mDesc = mDesc + new String(ch, start, length);
//        	 String ab = new String(ch, start, length); 
//        	 ab = ab.trim();
//        	 myParsedDataSet.addDesc(ab);
         }

    	}
   } 
	
}